package com.example.makotoshoda.mycamera3;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.hardware.Camera;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

/**
 * Created by makoto.shoda on 2014/12/19.
 */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private Camera mCamera = null;

    public CameraPreview(Context context) {
        super(context);

        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void setCamera(Camera cam) {
        mCamera = cam;
    }

     @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
         try {
             if (mCamera == null) return;
             mCamera.setPreviewDisplay(surfaceHolder);
         } catch (IOException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         }

     }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }

    public void release() {
        mCamera = null;
    }
}
